home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / perl / examples / dataurl < prev    next >
Encoding:
Text File  |  2000-08-24  |  12.9 KB  |  289 lines

  1. #!/usr/app/bin/perl
  2.  
  3. eval 'exec /usr/app/bin/perl  -S $0 ${1+"$@"}'
  4.     if 0; # not running under some shell
  5. # pcg@goof.com
  6. # this is not totally serious...
  7.  
  8. use Gimp;
  9. use Gimp::Fu;
  10. use Gimp::UI;
  11. use Fcntl;
  12.  
  13. sub encode_base64($) {
  14.     my $res = substr pack ("u", $_[0]), 1;
  15.     $res =~ s/\n.//mg;
  16.     $res =~ tr|` -_|AA-Za-z0-9+/|; #` # syntax-hiliting in emacs kanns nicht
  17.     my $padding = (3 - length($_[0]) % 3) % 3;
  18.     $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
  19.     $res;
  20. }
  21.  
  22. register "file_dataurl_save",
  23.          "Saves the image as many small tiles using data:-urls",
  24.          "=pod",
  25.          "Marc Lehmann",
  26.          "Marc Lehmann <pcg\@goof.com>",
  27.          "1999-11-20",
  28.          "<Save>/DATAURL",
  29.          "*",
  30.          [
  31.           [PF_SPINNER,    "tile_x",    "tile width",    32, [0, 8192, 1, 10]],
  32.           [PF_SPINNER,    "tile_y",    "tile height",    32, [0, 8192, 1, 10]],
  33.           [PF_RADIO,    "filetype",    "underlying file type", 0,
  34.                         [GIF => 0, JFIF => 1, PNG => 2]],
  35.          ],
  36.          sub {
  37.    my($img,$drawable,$filename,$filename2,$tx,$ty,$type) = @_;
  38.    my($new_img,$new_drawable);
  39.    my $max;
  40.    my $export = Gimp::UI::export_image ($new_img=$img, $new_drawable=$drawable, "DATAURL",
  41.                                         $type==0 ? EXPORT_CAN_HANDLE_INDEXED|EXPORT_CAN_HANDLE_ALPHA 
  42.                                       : $type==1 ? EXPORT_CAN_HANDLE_RGB|EXPORT_CAN_HANDLE_GRAY
  43.                                       : $type==2 ? EXPORT_CAN_HANDLE_RGB|EXPORT_CAN_HANDLE_GRAY|EXPORT_CAN_HANDLE_INDEXED
  44.                                       :            0 );
  45.    die __"export failed" if $export == EXPORT_CANCEL;
  46.  
  47.    my ($w,$h) = ($new_drawable->width, $new_drawable->height);
  48.  
  49.    my $tmp = Gimp->temp_name(".img~");
  50.  
  51.    sysopen FILE,$filename,O_CREAT|O_TRUNC|O_WRONLY or die __"Unable to open '$filename' for writing: $!\n";
  52.  
  53.    print FILE "<html><body>\n";
  54.  
  55.    init Progress __"Saving '$filename' as DATAURL...";
  56.  
  57.    my $media = $type==0 ? "gif"
  58.              : $type==1 ? "jpeg"
  59.              : $type==2 ? "png"
  60.              :            "";
  61.  
  62.    print FILE "<table width=$w cellspacing=0 cellpadding=0 border=0>";
  63.    for(my $y=0; $y<$h; $y+=$ty) {
  64.       my $wy = $h-$y < $ty ? $h-$y : $ty;
  65.       print FILE "<tr>";
  66.       for(my $x=0; $x<$w; $x+=$tx) {
  67.          my $wx = $w-$x < $tx ? $w-$x : $tx;
  68.  
  69.          my $img = $new_img->channel_ops_duplicate;
  70.          $img->crop($wx,$wy,$x,$y);
  71.          ($img->get_layers)[0]->file_gif_save  (($tmp)x2, 0, 0, 0, 0)                   if $type==0;
  72.          ($img->get_layers)[0]->file_jpeg_save (($tmp)x2, 0.7, 0, 1, 0, "", 0, 1, 0, 0) if $type==1;
  73.          ($img->get_layers)[0]->file_png_save  (($tmp)x2, 0, 1, 9)                      if $type==2;
  74.          $img->delete;
  75.  
  76.          my $data = do {
  77.             local(*TEMP,$/);
  78.             open TEMP, "<$tmp" or die __"Unable to read temporary image tile $tmp: $!";
  79.             <TEMP>;
  80.          };
  81.          unlink $tmp;
  82.  
  83.          $url = "data:image/$media;base64,".(encode_base64 $data);
  84.          $max = length($url) if length($url) > $max;
  85.  
  86.          print FILE "<td><img src=\"", $url, "\">";
  87.  
  88.          update Progress (($y*$w+$x*$ty)/($w*$h));
  89.       }
  90.    }
  91.  
  92.    print FILE "</table>\n";
  93.    print FILE "</html>\n";
  94.    close FILE;
  95.  
  96.    warn __"url size is too large ($max > 1024)\n" if $max > 1024;
  97.  
  98.    $new_img->delete if $export == EXPORT_EXPORT;
  99.    ();
  100. };
  101.  
  102. Gimp::on_query {
  103.    Gimp->register_save_handler("file_dataurl_save", "dataurl", "");
  104. };
  105.  
  106. exit main;
  107.  
  108. =head1 DATAURL FILE FORMAT
  109.  
  110. After reading rfc2397, which describes the C<data:> url scheme, I got the
  111. idea of embedding a normal image into a html document, without resorting
  112. to external files.
  113.  
  114. This is acomplished by embedding small tiles of the image directly
  115. into data:-urls. Since attribute values are by default limited to 1024
  116. bytes this limits the size of a tile to approximately 34x34 pixels (gif
  117. compression).
  118.  
  119. However, since rfc2397 is only a proposed standard, you might want to
  120. use much larger tile sizes (upto the image size), since most browsers
  121. generally do not care for the url length.
  122.  
  123. Browser compatibility list (send more results to pcg@goof.com ;)
  124.  
  125.   Netscape 3.x    displays broken image icons
  126.   Netscape 4.x    works on some configurations (communicator!),
  127.                 not on others (navigator!)
  128.   Lynx         displays the base64 code as text :(
  129.   MSIE 4    thousands of error messages in dialog boxes ;->
  130.   MSIE 5    shows broken image icon
  131.  
  132. =for nobody
  133.  
  134. <schmorp> yosh: cvs.gimp.org
  135. <yosh> I mean which "removed debugging crap" one
  136. <schmorp> yosh: or did you mean the debugging code in the Perl-Server?
  137. <yosh> yah
  138. <yosh> debian's perl whines about using -D since it's not compiled with -DDEBUGGING
  139. <schmorp> yosh: ah.. I it was ebing queried..
  140. <schmorp> yosh: yeah, but you did not see the <STDIN> that required people to press enter in colorhtml ;)
  141. <schmorp> anybody here with netscape 3.0? or msie 5.0? or any other graphical browser?
  142. <yosh> schmorp: it was in innerbevel, you must've fixed colorhtml before I had a chance to update
  143. <maswan> schmorp: ns3.0 here
  144. <schmorp> maswan: could you try something out? -> http://www.goof.com/pcg/marc/dataurl.html
  145. <schmorp> maswan: what do you see, especially below "dataurl filter"
  146. <schmorp> yosh: at least ;->
  147. *** Joins: Nether [lealanko@myntti.helsinki.fi] has joined #gimp
  148. *** Mode change for #gimp by XachBot: +o Nether
  149. <maswan> schmorp: slooow....
  150. <Stric> schmorp, evil thingie.. I'm loading it in win98/ie on a p60 8)
  151. *** Nether [lealanko@myntti.helsinki.fi] is now known as Neth_ZzZ
  152. <schmorp> that html page contains no external links, i.e. all images are embedded
  153. <schmorp> and yes, the colour-changing is bogging down most browsers
  154. <schmorp> i want to find out how many browsers support the data: url
  155. <Stric> I get a gazillion of [x] broken image thingies at the bottom half
  156. <maswan> schmorp: embedded? I got _lots_ of broken external images on the bottom half
  157. <schmorp> stric: which browser?
  158. <maswan> http://www.goof.com/pcg/marc/data:image/gif;base64,
  159.    R0lGODlhIgAiAOMAAA8UFDo/OGt9eJecj722oFxfVIl/a+vSrn5oTtiyg7qfe6SGZUlgX+LCliElIdCldCwAAAAAIgAiAAAE/vAd9dajKlV6FddY9TTd9ZzmSWkPi2quYiUsiJ4uXd+6i/e3Vk/XQpGMRVxj9
  160.    1omnE/n8nhqUGmv2lMnXY6EWBc0rNxah8IRjQQbeouw0dhKT5+562Cevu1BpWtcPy1WYHdmT2psXn1DjYF8fmdnhJCIB4VTmlyRdE5ggZhTnIFrXX6SDaKPZpN4XAd+sXV8f1GagFmmd7ZWmKaNo5ZDs1GoTh
  161.    k0sW5dkcGtJIeeuJ6kt7Nny7vTzXyiq2zGo7VS4NSqrdiIU8voJKLV04hypa5mB/DOw+K3qt147JQBfPLLU0Fh5IxhqdTHXh
  162. <Stric> schmorp, win98/ie (4? 5?)
  163. <schmorp> maswan: ok, so ns3.0 does not support it
  164. <schmorp> stric: i don't know how to find out ;9
  165. <Stric> schmorp, 5 appearantly...
  166. <schmorp> ok... ns3.0 and msie5 show broken image icons.
  167. <schmorp> msie displays one dialog box per link (evil)
  168. <schmorp> lynx errornously displays the base64 data
  169. <yosh> haha
  170. <schmorp> so only netscape 4 displays it correctly
  171. <yosh> that is too evil
  172. <yosh> tried mozilla?
  173. *** Quits: _vicious_:#gimp [~jirka@dt062nd1.san.rr.com] (GEGL!)
  174. <schmorp> yosh: mozilla does not run, did not run and will probably never run
  175. <schmorp> yosh: everytime I come here and somebody talks about mozilla he says "well, it is quite cool"
  176. <schmorp> yosh: everytime _I_ try it ends in an endless loop after 5 seconds or so
  177. <yosh> heh
  178. <Stric> you must be broken then
  179. <schmorp> yosh: but I guess mozilla will do it as well
  180. <yosh> schmorp: I get broken images with NS 4.7
  181. <schmorp> yosh: really? thats cool... it works with ns 4.06 (here) and ns 4.6 (my friend)
  182. <schmorp> yosh: on linux?
  183. <yosh> yes
  184. <schmorp> yosh: i wonder why they removed support for it..
  185. <sjburges> I get broken stuff with 4.51
  186. *** TomR^AwaY [~tomr@judas.aceldama.com] is now known as Tommer
  187. <yosh> hey Tommer
  188. <Tommer> Works for me on NS 4.71 on LinuxPPC
  189. <Tommer> Hullo yosh
  190. <schmorp> tommer: really?
  191. <Tommer> dataurl is very cute, schmorp
  192. <Tommer> Yup
  193. * yosh tries mozilla
  194. <schmorp> something must be broken... 4.06 yes, 4.51 no, 4.6 yes, 4.7 no, 4.71 yes...
  195. <schmorp> I see a pattern...
  196. <Tommer> Oh, you mean the ($ver*100)%2 == (dataurl works) relation?
  197. <Tommer> Oh wait, 4.6 doesn't match that.
  198. * maswan tries ns4.61 on aix
  199. <schmorp> oh, that 4.6 is probably measurement error
  200. <Tommer> schmorp, for the next trick, do <frameset> <frame src=dataurl:> <frame src=dataurl:> </frameset> :)
  201. <yosh> haha
  202. <schmorp> hmm... it works with 4.51 here....
  203. <maswan> eeek.
  204. <Tommer> how about <body background="dataurl:...">
  205. *** Quits: tigert:#gimp [tigert@fun112.koivukyla.hoas.fi] (Ping timeout: 660 seconds)
  206. <schmorp> tommer: hmm... now that would be cool... it would not even need base64 encoding, and would probably work even with lynx
  207. <maswan> this one is sloow for some reason.
  208. <tc> hmm
  209. *** Topic change by sjburges on #gimp: its abuse html night
  210. <Tommer> schmorp: yes, as long as you used " everywhere you'd be fine :)
  211. <schmorp> maswan: "aix"....
  212. <tc> could it be that clinton only knows of the internet from al gore?
  213. <Stric> schmorp, low mhz combined with netscape on 24bpp
  214. <maswan> hmm.. or ssh. oops. :)
  215. <tc> "reduce the gap between rich and poor by giving everyone internet access"
  216. <Tommer> Does anyone have red+blue 3D glasses handy?
  217. <tc> yay clinton. you da man
  218. <maswan> schmorp: works on this one
  219. <schmorp> stric: but since netscaope can't display colour icons in 24bpp it should be much faster ;->
  220. <schmorp> maswan: thanks ;)
  221. <Stric> schmorp, I haven't had that problem on non-linux
  222. <maswan> hmm.. it is estimating speed at 2k/s
  223. <schmorp> tommer: the problem is that urls are max. 1k in size
  224. <Stric> schmorp, I correct that to aix/irix/solaris
  225. <schmorp> tommer: but on the browsers that support it, dataurls can be much longer
  226. <maswan> which means much time for that file
  227. <Tommer> schmorp: can't you specify encoding=base64 _and_ encoding=gzip?
  228. <yosh> hum, mozilla sucks ;)
  229. <schmorp> tommer: i can't specify an encoding per se. if, then it must be a netscape extension
  230. <schmorp> tommer: maybe image/gzip-jpg or something....
  231. <Tommer> schmorp: oh, so you can only specify a content type, not an encoding? I was guessing about the encoding thing.
  232. * maswan tries irix netscape 4.something
  233. <maswan> 4.61 here too
  234. <schmorp> tommer: the base64 is specified as ";base64", so it is somethign special
  235. <schmorp> tommer: but since I can specify charset=xxx, maybe I can also add encoding=gzip...
  236. <maswan> works that too
  237. <schmorp> tommer: BUT... if I can use gzip, then I cna use long urls. Then I can use png, and then i don't need gzip
  238. <Tommer> schmorp: just think, you could put a whole site in one page with framesets and dataurl :)
  239. <schmorp> yosh: mozilla sucks, yes, but does it display it?
  240. <Tommer> How are gzip and long urls related, schmorp?
  241. <yosh> schmorp: it segfaults
  242. <yosh> ;)
  243. <schmorp> tommer: hmm... I'm thinking images only...
  244. <schmorp> tommer: if you think text then encoding=gzip makes sense
  245. <schmorp> tommer: I also haven'T tried wether these urls work in frameset and normal anchor elements
  246. <schmorp> but actually, the colorhtml filter is much more portable
  247. <yosh> yeah, it just takes aching long to render and scroll
  248. <Tommer> schmorp: how about <img lowsrc="data:image/gif;base64, ... " src="/images/foo.gif"> ?
  249. <schmorp> yes... maybe when this type of encapsulation becomes more often used... at least
  250. <schmorp> we can claim that msie is not rfc2397 compatible
  251. <ole_zzz> xachbot, seen dv
  252. -XachBot:#gimp- I last saw dv (veillard@home5.inrialpes.fr) 7d 13h 17m ago [quit: (xchat exiting..)]
  253. <Stric> schmorp, and who will care? 8)
  254. <schmorp> tommer: that might even be a useful usage!!
  255. *** Joins: JohnP [me2@d027.pnj.early.com] has joined #gimp
  256. *** Mode change for #gimp by XachBot: +o JohnP
  257. <JohnP> Hey!
  258. <Tommer> schmorp: but lowsrc is an evil Netscape invention :)
  259. *** Joins: tigert [~tigert@fun112.koivukyla.hoas.fi] has joined #gimp
  260. *** Mode change for #gimp by Wilber: +o tigert
  261. <Stric> tigpoo
  262. <JohnP> tigert!
  263. <Tommer> schmorp: <img lowsrc="data:image/xbm;base64, ... " src="/images/foo.gif">
  264. <schmorp> tommer: does the lowsrc image have to have the same size/resolution?
  265. <Tommer> Hmm, does <img src="internal-gopher-menu"> and such work?
  266. <maswan> hmm.. sleep. now.
  267. <Tommer> schmorp: lowsrc _should_ have the same size/resolution. if you specify width and height then the lowsrc will be resized of course.
  268. <schmorp> tommer: .... and since it's netscaoe only we do not need to care
  269. <Tommer> schmorp: yes, if you're abusing HTML you may as well go whole hog.
  270. <schmorp> tommer: hey, it's a "proposed standard" ;)
  271. <yosh> heh
  272. <Tommer> A nice thing about NS on the Mac is that if you shrink a mono image then it renders it antialiased :)
  273. Tommer> Pull out your red+blue 3D glasses and go to: http://www.aceldama.com/~tomr/wx/tomrcam-3d-2.jpg
  274. <schmorp> yosh: rfc2397 "data: url scheme"
  275. *** Joins: jlb [~jlbec@slip-32-101-161-237.nc.us.prserv.net] has joined #gimp
  276. *** Mode change for #gimp by XachBot: +o jlb
  277. <schmorp> tommer: looks flashy (no 3d cam here ;)
  278. <schmorp> ok, bye all!
  279. <Tommer> ttyl schmorp
  280. <schmorp> sjburges: have a nice time with your new g/f!
  281. <yosh> interesting
  282. <Tommer> schmorp: you need 3D glasses
  283. <yosh> ok, food
  284. *** yosh [manish@graft.XCF.Berkeley.EDU] is now known as yosh_food
  285. <schmorp> tommer: if you do some weird things with urls, drop me a note ;->
  286. <schmorp> tommer: ah, yes, _glasses_ i meant
  287.  
  288. =cut
  289.